//
//  ColumnDrugCollectionViewCell.m
//  MedicineBox
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ColumnDrugCollectionViewCell.h"
#import "ListCollectionViewCell.h"
#import "AsyncConnection.h"
#import "HomeModel.h"
#import "MJRefreshNormalHeader.h"
#import "MJRefreshAutoNormalFooter.h"
#import "DKNightVersion.h"
#import "UIImageView+NightImage.h"

@interface ColumnDrugCollectionViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UICollectionView *collectView;
@property (nonatomic, retain) NSString *urlStr;
@property (nonatomic, assign) NSInteger i;
@end


@implementation ColumnDrugCollectionViewCell

-(void)dealloc
{
    [_array release];
    [_urlStr release];
    [_collectView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [DKNightVersionManager addClassToSet:self.class];
        self.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.array = [NSMutableArray array];
        [self createCollectView];
    }
    return self;
}
#pragma mark -
#pragma mark 数据处理
- (void)valueWithUrl:(NSString *)url
{
    self.urlStr = url;
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    [mDic setObject:@"app" forKey:@"app_key"];
    [mDic setObject:@"0" forKey:@"curPage"];
    [mDic setObject:@"20" forKey:@"limit"];
    [mDic setObject:@"9DFAAD5404FCB6168EA6840DCDFF39E5" forKey:@"sign"];
    [mDic setObject:@"" forKey:@"tags"];
    [mDic setObject:url forKey:@"CategoryName"];
    
    [self.array removeAllObjects];
    [AsyncConnection asyncConnectionWithUrl:MB_DrugColumn name:url parameters:mDic block:^(id data) {
        NSArray *list = [[data objectForKey:@"results"] objectForKey:@"List"];
        
        for (NSDictionary *dic in list) {
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            [self.array addObject:model];
        }
        [self.collectView reloadData];
    } get:@"get"];
}


#pragma mark - 
#pragma mark 创建collectView
- (void)createCollectView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) collectionViewLayout:layout];
   
    [self.contentView addSubview:self.collectView];
    self.collectView.backgroundColor = [UIColor clearColor];
    self.collectView.delegate = self;
    self.collectView.dataSource = self;
    
     self.collectView.nightBackgroundColor = UIColorFromRGB(0x343434);
    
    [self.collectView registerClass:[ListCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    [_collectView release];
    [layout release];
    
    [self createRefresh];
}
#pragma mark -
#pragma mark 上拉加载下拉刷新
- (void)createRefresh
{
    _i = 1;
// 刷新
    self.collectView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self.collectView.header beginRefreshing];
        
        [self valueWithUrl:self.urlStr];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.collectView reloadData];
            // 结束刷新
            [self.collectView.header endRefreshing];
        });

    
    }];
// 加载
    self.collectView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        
        NSString *page = [NSString stringWithFormat:@"%ld",_i * 20];

        NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
        [mDic setObject:@"app" forKey:@"app_key"];
        [mDic setObject:page forKey:@"curPage"];
        [mDic setObject:@"20" forKey:@"limit"];
        [mDic setObject:@"9DFAAD5404FCB6168EA6840DCDFF39E5" forKey:@"sign"];
        [mDic setObject:@"" forKey:@"tags"];
        [mDic setObject:self.urlStr forKey:@"CategoryName"];
       
        [AsyncConnection asyncConnectionWithUrl:MB_DrugColumn name:self.urlStr parameters:mDic block:^(id data) {
            NSArray *list = [[data objectForKey:@"results"] objectForKey:@"List"];
            
            for (NSDictionary *dic in list) {
                HomeModel *model = [[HomeModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.array addObject:model];
            }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.collectView reloadData];
                // 结束刷新
                
                [self.collectView.footer endRefreshing];
            });
            [self.collectView.footer resetNoMoreData];
        } get:@"get"];
        
        self.i++;
        
    }];

}

#pragma mark - 
#pragma mark item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
#pragma mark -
#pragma mark 自定义cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
    cell.nightBackgroundColor = [UIColor clearColor];
    if (self.array.count > 0) {
        [cell valueWithModel:[self.array objectAtIndex:indexPath.item]];
    }
    return cell;
}
#pragma mark - 
#pragma mark item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((collectionView.frame.size.width - 50) / 2, 150);
}
#pragma mark - 
#pragma mark 点击进入药品详细信息 消息通知
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *str = [NSString stringWithFormat:@"%ld", (long)indexPath.item];
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    [mDic setObject:[str mutableCopy] forKey:@"item"];
//    [mDic setValue:[str mutableCopy] forKey:@"item"];
//    [mDic setValue:[self.array mutableCopy] forKey:@"array"];
    [mDic setObject:self.array forKey:@"array"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KNOTIFIACTION" object:nil userInfo:mDic];
}

@end
